package com.controller;


import com.Dto.BeginAndEndSessionDto;
import com.Dto.GuidanceMessageDto;
import com.Dto.GuidanceRecordDto;
import com.Dto.TempRecordDto;
import com.common.Result;
import com.pojo.CounselingSession;
import com.pojo.GuidanceMessage;
import com.pojo.GuidanceSession;
import com.service.CounselorService;
import com.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("Messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 开始和结束会话
     * @param beginAndEndSessionDto
     * @return
     */
    @PostMapping("BeginAndEndSession")
    public Result<Integer> BeginAndEndSession(@RequestBody BeginAndEndSessionDto beginAndEndSessionDto){
        Result<Integer> result = new Result();
        Integer personOneId = beginAndEndSessionDto.getPersonOneId();
        Integer personTwoId = beginAndEndSessionDto.getPersonTwoId();

        String currentTimeString = beginAndEndSessionDto.getCurrentTime();
        LocalDateTime currentTime = LocalDateTime.parse(currentTimeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Integer ConversationType = beginAndEndSessionDto.getConversationType();
        Integer ConversationState = beginAndEndSessionDto.getConversationState();
        int sessionId;
        try{
            if(ConversationType == 0)
            {
                if(ConversationState == 0){
                    sessionId = messageService.createCounselingSession(personOneId,personTwoId,currentTime,ConversationState);
                }
                else{
                    sessionId = messageService.endCounselingSession(personOneId,personTwoId,currentTime);
                }
            }else{
                if(ConversationState == 0){
                    sessionId = messageService.createGuidanceSession(personOneId,personTwoId,currentTime,ConversationState);
                }
                else{
                    sessionId = messageService.endGuidanceSession(personOneId,personTwoId,currentTime);
                }
            }
        result.setSuc(sessionId);
        }catch (Exception e){
            log.error("失败：【{}】",e.getMessage());
            result.setFail("失败");
        }
        return result;

    }

    @PostMapping("receiveHelpSingleMessage")
    public Result<Boolean> receiveSingleMessage(@RequestBody GuidanceMessageDto guidanceMessageDto){
        Result<Boolean> result = new Result();
        Integer from = guidanceMessageDto.getFrom();
        Integer to = guidanceMessageDto.getTo();
        Integer sessionId = guidanceMessageDto.getSessionId();
        String content = guidanceMessageDto.getContent();
        Integer type = guidanceMessageDto.getType();

        String timeString = guidanceMessageDto.getTime();
        LocalDateTime time = LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        try{
             boolean flag = messageService.receiveSingleMessage(from, to, sessionId, content, time, type);
              result.setSuc(flag);
        }catch (Exception e){
            log.error("插入失败：【{}】",e.getMessage());
            result.setFail("插入失败");
        }
        return result;

    }

    @GetMapping("getGuidanceRecords")
    public Result<List<GuidanceRecordDto>> getGuidanceRecords(@RequestParam("counselorId") Integer counselorId, @RequestParam("supervisorId") Integer supervisorId, @RequestParam("time") String time){
        Result<List<GuidanceRecordDto>> listResult = new Result<>();

        //Integer counselorId = tempRecordDto.getCounselorId();
        //Integer supervisorId = tempRecordDto.getSupervisorId();
        //String timeString  = tempRecordDto.getTime();
        LocalDateTime time1 = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        System.out.print(time1);

        try{
            List<GuidanceRecordDto> messageList = messageService.getGuidanceRecords(counselorId,supervisorId,time1);
            listResult.setSuc(messageList);
         }catch (Exception e){
            log.error("获取失败：{}",e.getMessage());
            listResult.setFail("获取失败");
         }
        return listResult;
    }
}






